﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.Models;

public partial class FileManager_FileManager : System.Web.UI.Page
{
    List<FileInfo> deptList = new FileInfoManager().GetAllFileInfo();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<FileInfo> list = new FileInfoManager().GetAllFileInfo();
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();
            foreach (FileInfo item in deptList)
            {
                if (item.ParentId == 0)
                {
                    if (item.FileType.FileTypeId == 1)
                    {
                        TreeNode tn = new TreeNode();
                        tn.Text = item.FileName;
                        tn.Value = item.FileId.ToString();

                        tn.ImageUrl = item.FileType.FileTypeImage;

                        this.TreeView1.Nodes.Add(tn);
                        InitTreeByList(tn);

                    }
                }
            }
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    private void InitTreeByList(TreeNode TN)
    {
        foreach (FileInfo item in deptList)
        {
            if (item.ParentId.ToString() == TN.Value)
            {
                if (item.FileType.FileTypeId == 1)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = "&nbsp;" + item.FileName;
                    tn.Value = item.FileId.ToString();
                    //tn.NavigateUrl = item.NodeURL;
                    tn.ImageUrl = item.FileType.FileTypeImage;
                    TN.ChildNodes.Add(tn);
                    InitTreeByList(tn);
                }
            }


        }
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(TreeView1.SelectedNode.Value);
        List<FileInfo> list = new FileInfoManager().GetAllFileInfoByParentId(id);
        FileInfo file = new FileInfoManager().GetAllFileInfoByFileId(id);
        this.txtAddress.Text = file.FilePath;
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
        if (e.CommandName=="click")
        {
            GridViewRow dv = (GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent);
            int id = Convert.ToInt32(GridView1.DataKeys[dv.RowIndex].Value);
            FileInfo file = new FileInfoManager().GetAllFileInfoByFileId(id);
            if (file.FileType.FileTypeId == 1)
            {
                List<FileInfo> list = new FileInfoManager().GetAllFileInfoByParentId(file.ParentId);
                this.GridView1.DataSource = list;
                this.GridView1.DataBind();
            }
            else
            {
                Response.Redirect(string.Format("FileAttribute.aspx?id={0}",id));
            }
        }
        if (e.CommandName=="Att")
        {
            GridViewRow dv = (GridViewRow)(((ImageButton)(e.CommandSource)).Parent.Parent);
            int id = Convert.ToInt32(GridView1.DataKeys[dv.RowIndex].Value);
            FileInfo file = new FileInfoManager().GetAllFileInfoByFileId(id);
            Response.Redirect(string.Format("FileAttribute.aspx?id={0}",id));
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id =Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        FileInfo file = new FileInfoManager().GetAllFileInfoByFileId(id);
        FileInfo file2 = new FileInfo();
        file2.CreateDate = file.CreateDate;
        file2.FileId = file.FileId;
        file2.FileName = file.FileName;
        file2.FileOwner = file.FileOwner;
        file2.FilePath = file.FilePath;
        file2.FileType = file.FileType;
        file2.IfDelete = 1;
        file2.ParentId = file.ParentId;
        file2.Remark = file.Remark;
        new FileInfoManager().UpdateFileInfo(file2);
        List<FileInfo> list = new FileInfoManager().GetAllFileInfo();
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }
    protected void imgbtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(string.Format("FileAttribute.aspx?pos={0}&nodeid={1}",this.txtAddress.Text.Trim(),TreeView1.SelectedNode.Value));
    }
    protected void addFolder_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(string.Format("AddFolder.aspx?pos={0}&nodeid={1}", this.txtAddress.Text.Trim(), TreeView1.SelectedNode.Value));
    }
    protected void imgbtnSerach_Click(object sender, ImageClickEventArgs e)
    {

    }
}